package cs2340.todo.view;

import java.util.LinkedList;
import java.util.List;

import cs2340.todo.model.TODOItem;
import cs2340.todo.model.TODOManager;
import cs2340.todo.model.User;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

/**
 *
 * @author Vertigo (43)
 *
 */
public class TODOManagerActivity extends ListActivity {

	protected static final int CREATE_REQUEST_CODE = 1;
	private Button btnAdd;
	private TextView lblTitle, txtItems;
	private User user;

	private TODOManager items;

	private ListAdapter adapter;
	private List<TODOItem> data;
	//private String data;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//gets user information from login screen
		Bundle getU = getIntent().getExtras();
		user = new User(getU.getString("name"), getU.getString("username"), getU.getString("email"), getU.getString("password"));        

		items = new TODOManager(TODOManagerActivity.this);
		items.open();
		data = items.getUserItems(user);
		items.close();
		adapter = new ListAdapter(this, R.layout.list_item, data);
		setListAdapter(adapter);
		setContentView(R.layout.main);   

		ActionBar bar = getActionBar();
		bar.setDisplayShowTitleEnabled(false);

		btnAdd = (Button) findViewById(R.id.btnAdd);
		lblTitle = (TextView) findViewById(R.id.lblTitle);
		lblTitle.setText("User: " + user.getUsername());

		btnAdd.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent newItem = new Intent(TODOManagerActivity.this, NewItemActivity.class);
				Bundle u = new Bundle();
				u.putString("name", user.getName());
				u.putString("username", user.getUsername());
				u.putString("password", user.getPassword());
				u.putString("email", user.getEmail());
				newItem.putExtras(u);
				startActivityForResult(newItem, 0);
			}
		});
	}
	
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0 && resultCode == RESULT_OK) {
			items.open();
			this.data = items.getUserItems(user);
			items.close();	
			ListAdapter updateAdapter = new ListAdapter(this, R.layout.list_item, this.data);
			setListAdapter(updateAdapter);
		}
	}


	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}


	private class ListAdapter extends ArrayAdapter<TODOItem> {

		private List<TODOItem> items;

		public ListAdapter(Context context, int textViewResourceId, List<TODOItem> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.list_item, null);
			}
			TODOItem o = items.get(position);
			if (o != null) {
				TextView title = (TextView) v.findViewById(R.id.txtTitle);
				TextView time = (TextView) v.findViewById(R.id.txtTime);
				if (title != null) {
					title.setText(o.getTitle());
				}
				if(time != null){
					time.setText("Due: "+ o.getTime());
				}
			}
			return v;
		}
	}
}